package com.gitee.dupe.config;

import com.gitee.dolt.database.datasource.BaseDatabaseConfiguration;
import com.gitee.dolt.database.properties.DatabaseProperties;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.aop.Advisor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.interceptor.TransactionInterceptor;


/**
 * @author LYH
 */
@Configuration
@Slf4j
@EnableTransactionManagement
@MapperScan(
        basePackages = {"com.gitee.dupe.*.dao",},
        annotationClass = Repository.class
)
@Aspect
public class DupeDatabaseAutoConfiguration extends BaseDatabaseConfiguration {


    public DupeDatabaseAutoConfiguration(DatabaseProperties databaseProperties) {
        super(databaseProperties);
    }

    @Bean
    protected TransactionInterceptor dupeTransactionInterceptor(@Nullable TransactionManager txManager) {
        TransactionInterceptor interceptor = new TransactionInterceptor();
        interceptor.setTransactionAttributeSource(transactionAttributeSource());
        if (txManager != null) {
            interceptor.setTransactionManager(txManager);
        }
        return interceptor;
    }

    @Override
    @Bean
    protected Advisor txAdviceAdvisor(@Qualifier(value = "dupeTransactionInterceptor") TransactionInterceptor ti) {
        return super.txAdviceAdvisor(ti);
    }
}
